class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int left = 0, right = nums.size() - 1, ans = 0;
        while (left <= right) {
            while (left < right&& nums[right] == val)
                right--,ans++;
            nums[left] == val ? swap(nums[left++] ,nums[right--]), ans++ : left++;
        }
        return nums.size() - ans;
    }
};
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int left =  1,right = 1,ans = 0;
        while(right < nums.size()){
            if(nums[right] != nums[right - 1]){
                nums[left++] = nums[right];
            }
            right++;
        }
        return left;
    }
};
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int left = 0,right = 0,ans = 0;
        for(;right < nums.size();right++)
        {
            if(left < 2 || nums[left - 2] != nums[right])
            {
                nums[left++] = nums[right];
            }
        }
        return left;
    }
};
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int i0 = 0;
        for (auto& x : nums) {
            if (x) {
                swap(x, nums[i0]);
                i0++;
            }
        }
    }
};
class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        int to = 0;
        for (auto& x : nums) {
            if (x%2 == 0) {
                swap(x, nums[to]);
                to++;
            }
        }
        return nums;
    }
};
class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& nums) {
        int j = 1, n = nums.size();
        for (int i = 0; i < nums.size();) {
            while (i < n && nums[i] % 2 == 0)
                i += 2;
            while (j < n && nums[j] % 2 == 1)
                j += 2;

            if (i < n && j < n)
                swap(nums[i], nums[j]);
            else
                break;
        }
        return nums;
    }
};
class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                nums[i] *=2, nums[i + 1] = 0;
            }
        }
        int left = 0;
        for (auto& x : nums) {
            if (x) {
                swap(nums[left++], x);
            }
        }
        return nums;
    }
};
class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int left = 0,n = arr.size(),right = n-1;
        for(;left < right;left++){
            if(arr[left] == 0)
                right--;
        }
        int end = n - 1;
        if(left == right && arr[left] == 0)
            arr[end--] = 0,right--;
        for(;right>=0;right--){
            arr[end--] = arr[right];
            if(arr[right] == 0)
            {
                arr[end--] = 0;
            }
        }
    }
};
// 1 0 3 0 4 0 0
// 1 0 0 3 0 0 4